package com.futureCloudMOM.controller.core.web.basic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.core.page.TableDataInfo;
import com.futureCloudMOM.controller.core.controller.BaseApiController;
import com.futureCloudMOM.core.domain.BasicProcess;
import com.futureCloudMOM.core.domain.BasicProcessAction;
import com.futureCloudMOM.core.dto.BasicProcessDto;
import com.futureCloudMOM.core.service.BasicProcessActionService;
import com.futureCloudMOM.core.service.BasicProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 文件名 BasicProcessController
 *
 * @since 2023/5/29 9:21
 */
@Api(tags = "基础工序")
@RestController
@RequestMapping("basic/process")
public class BasicProcessController extends BaseApiController<BasicProcess, BasicProcessDto, BasicProcessService> {

    public BasicProcessController(BasicProcessService service) {
        super(service);
    }

    @Autowired
    private BasicProcessActionService basicProcessActionService;

    @Override
    public void addExt(BasicProcessDto basicProcessDto) {
        List<BasicProcessAction> actions;
        if (!CollectionUtils.isEmpty(actions = basicProcessDto.getActions())) {
            actions.forEach(basicProcessAction -> {
                basicProcessAction.setProcessId(basicProcessDto.getId());
                basicProcessActionService.save(basicProcessAction);
            });
        }
    }

    @Override
    public void updateExt(BasicProcessDto basicProcessDto) {
        List<BasicProcessAction> actions;
        if (!CollectionUtils.isEmpty(actions = basicProcessDto.getActions())) {
            actions.forEach(basicProcessAction -> {
                Long id = basicProcessAction.getId();
                if (Objects.isNull(id)) {
                    basicProcessAction.setProcessId(basicProcessDto.getId());
                }
                basicProcessActionService.saveOrUpdate(basicProcessAction);
            });
        }
    }

    @Override
    public void deleteExt(Long[] ids) {
        basicProcessActionService.remove(new LambdaQueryWrapper<BasicProcessAction>().in(BasicProcessAction::getProcessId, (Object[]) ids));
    }

    @ApiOperation("查询工序--可根据名称和编码筛选数据--可分页")
    @PostMapping("/getProcessByNameAndCode")
    public TableDataInfo getProcessByNameAndCode(@RequestBody BasicProcess process){
        startPage();
        return getDataTable(service.getProcessByNameAndCode(process));
    }
}
